<template>
    <div class="pad20">
        <div>
            <el-card class="box-card">
                <el-breadcrumb separator-class="el-icon-arrow-right">
                    <el-breadcrumb-item>专区管理</el-breadcrumb-item>
                </el-breadcrumb>
            </el-card>
            <!-- 分隔符-->
            <el-divider></el-divider>
            <el-form :inline="true" :model="searchformData" class="demo-form-inline" size="mini">
                <el-form-item label="商品名称">
                    <el-input v-model="searchformData.goodsName" placeholder="请输入专区名称"></el-input>
                </el-form-item>
                <el-form-item label="状态：" >
                    <el-select v-model="searchformData.isDisable" class="auto-width" clearable filterable placeholder="状态" style="width: 85px">
                        <el-option
                                v-for=" item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button icon='el-icon-search'size="mini" type="primary" @click="handleCheck">查询</el-button>
                    <el-button icon='el-icon-refresh' size="mini"  @click="handleReset">重置</el-button>
                    <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" @click="handleAddRow()">添加</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-table v-loading="ctrl.loading" size="medium" :data="tableData" stripe border style="width: 100%">
                <el-table-column type="index" label="序号" width="50">
                </el-table-column>
                <el-table-column prop="goodsName" label="商品名称" width="250">
                </el-table-column>
                <el-table-column prop="zoneName" label="专区名称" width="250">
                </el-table-column>
                <el-table-column
                        width="170"
                        prop="isDisable"
                        label="状态"
                        align="center">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.isDisable"
                                :active-value="1"
                                :inactive-value="0"
                                active-color="#ff4949"
                                inactive-color="#13ce66"
                                active-text="禁用"
                                inactive-text="正常"
                                @change="changeStatus(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="gmtCreate" label="添加时间">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="250"
                        align="center">
                    <template slot-scope="scope">
                        <ul class="list-item-actions">
                            <li>
                                <el-button :disabled="scope.row.isDisable==0" type="danger" @click="handleDelRow(scope.row.id)" size="mini" >删除</el-button>
                            </li>
                        </ul>
                    </template>
                </el-table-column>
            </el-table>
            <div class="mgt20">
                <el-pagination
                        background
                        style="float: right;margin-top: 20px; margin-bottom: 22px"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[1, 5, 10, 15]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
        </div>
        <add :zoneId="zoneId" :visible="ctrl.addDialogVisible"  :title="ctrl.dialogTitle" @close-callback="closeCallback"></add>
    </div>

</template>
<script>

    import Add from "./add";
    export default {
        components: {Add},
        data() {
            return {
                searchformData:{
                    goodsname:"",
                    isDisable:null
                },
                currentPage:1,
                pageSize:10,
                //总条数
                total:0,
                ctrl: {
                    loading: false,
                    addDialogVisible: false,
                    editDialogVisible: false
                },
                map: {},
                formData: {},
                // 表单数据, 例如新增编辑子项，页面表单
                zoneId: '',
                tableData: [],
                page: {
                    beginPageIndex: 1,
                    pageCurrent: 1,
                    endPageIndex: 8,
                    pageSize: 20,
                    totalCount: 0,
                    totalPage: 0
                },
                //下拉选项框的数据
                options:[{
                    value: '0',
                    label: '正常'
                },
                    {
                        value: '1',
                        label: '禁用'
                    },
                ],
            }
        },
        mounted() {
            this.searchformData.zoneid = this.$route.query.zoneId
            this.init(this.searchformData)
        },
        methods: {

            //改变当前页数
            handleSizeChange(val) {
                this.pageSize=val;
                this.init();
            },
            //改变当前每页的条数
            handleCurrentChange(val) {
                this.currentPage=val;
                this.init();
            },

            //改变状态
            changeStatus(row) {
                //改变状态
                var that=this;
                this.$http.put("/firstpage/api/zoneGoods/updateZoneGoods",row).then(function (resp) {
                    if (resp.data.code===2000){
                        that.$message({
                            message:resp.data.msg,
                            type:"success"
                        })
                    }else{
                        that.$message({
                            message:resp.data.msg,
                            type:"error"
                        })
                    }
                    that.init()
                })
            },
            //新增
            handleAddRow(zoneId) {
                this.zoneId = this.$route.query.zoneId
                this.ctrl.dialogTitle = '新增'
                this.ctrl.addDialogVisible = true
            },
            //删除
            handleDelRow(id) {
                this.$confirm(`确定删除这条数据?`, '我要删除', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var that=this;
                    this.$http.get("/firstpage/api/zoneGoods/delZoneGoods/"+id).then(function (resp) {
                        if (resp.data.code===2000){
                            that.$message({
                                message:resp.data.msg,
                                type:"success"
                            })
                        }else{
                            that.$message({
                                message:resp.data.msg,
                                type:"error"
                            })
                        }
                        that.init();
                    })
                })
            },
            // 关闭编辑弹窗回调
            closeCallback() {
                this.ctrl.dialogVisible = false
                this.ctrl.addDialogVisible = false
                this.ctrl.editDialogVisible = false
                this.reload()
            },
            // 查询
            handleCheck() {
                this.init()
            },
            // 重置查询条件
            handleReset() {
                this.reload()
            },
            reload() {
                this.searchformData = {}
                this.searchformData.zoneid = this.$route.query.zoneId
                this.init()
            },
            //编辑
            handleUpdateRow(data) {
                this.formData = data
                this.ctrl.dialogTitle = data.courseName + '——编辑'
                this.ctrl.editDialogVisible = true
            },
            init() {
                // this.searchformData.pageCurrent=this.currentPage;
                // this.searchformData.pageSize = this.pageSize;
                // var that=this;
                // this.searchformData.zoneId = this.$route.query.zoneId
                // this.$http.post(`vd-index/index/zone-goods/findAllZoneGoods`,this.searchformData).then(function (resp) {
                //     if (resp.data.code===2000){
                //         that.tableData=resp.data.data.records;
                //         console.log(that.tableData)
                //         that.total=resp.data.data.total;
                //     }
                //     if (resp.data.code===5000){
                //         that.$message({
                //             message:resp.data.msg,
                //             type:"error",
                //         })
                //         that.tableData=[];
                //     }
                // })
                this.searchformData.zoneid = this.$route.query.zoneId
                this.$http.post("/firstpage/api/zoneGoods/getZoneGoods/"+this.currentPage+"/"+this.pageSize,this.searchformData).then(res=>{
                    this.tableData=res.data.data.records
                    this.currentPage=res.data.data.current
                    this.total=res.data.data.total
                })

            }
        }
    }
</script>
